package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingListService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@RequestMapping("/cargo/packingList")
@Controller
public class PackingListController extends BaseController {

    @Reference(check = false, retries = 0)
    private PackingListService packingListService;
    @Reference(check = false, retries = 0)
    private ExportService exportService;
    @Reference(check = false, retries = 0)
    private ExportProductService exportProductService;

    /*
      作用：进入装箱单的添加
      路径： /cargo/packingList/toAdd.do
      参数： 报运单ID集合
      返回值： cargo/packingList/packingList-add.jsp
   */
    @RequestMapping("/toAdd")
    public String toAdd(String id) {
        System.out.println(id);
        String[] exportIds = id.split(",");//取出该报运单对应的购销合同的id
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(exportIds));
        List<Export> exportList = exportService.findAll(exportExample);
        //这里写3个地方一致的逻辑
        HashSet<String> set = new HashSet<>();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < exportList.size(); i++) {
            Export export = exportList.get(i);
            String shipmentPort = export.getShipmentPort();
            String destinationPort = export.getDestinationPort();
            String consignee = export.getConsignee();
            set.add(shipmentPort);
            set.add(destinationPort);
            set.add(consignee);
            list.add(shipmentPort);
            list.add(destinationPort);
            list.add(consignee);
        }
        //当个数等于3时，跳转成功,现在默认注释
        /*
        if(set.size()!=3){
            return "cargo/packingList/packingList-error";
        }
        */
        String exportNos = "";
        Double totalVolume = 0.0;      //总体积
        Double netWeights = 0.0;       //总净重
        Double grossWeights = 0.0;     //总毛重
        PackingList packingList = new PackingList();
        for (Export export : exportList) {
            //报运合同号的结合成的字符串；
            exportNos += export.getCustomerContract() + ",";
            ExportProductExample exportProductExample = new ExportProductExample();
            exportProductExample.createCriteria().andExportIdEqualTo(export.getId());
            List<ExportProduct> exportProducts = exportProductService.findAll(exportProductExample);
            if (exportProducts != null) {
                for (ExportProduct exportProduct : exportProducts) {
                    if (exportProduct.getSizeLength() == null || exportProduct.getSizeLength() == 0 ||
                            exportProduct.getSizeWidth() == null || exportProduct.getSizeWidth() == 0 ||
                            exportProduct.getSizeHeight() == null || exportProduct.getSizeHeight() == 0 ||
                            exportProduct.getCnumber() == null || exportProduct.getCnumber() == 0 ||
                            exportProduct.getGrossWeight() == null || exportProduct.getGrossWeight() == 0 ||
                            exportProduct.getNetWeight() == null || exportProduct.getNetWeight() == 0
                            ) {
                        return "cargo/packingList/packingList-error";
                    }
                    //体积=报运货物的长*宽*高*个数
                    totalVolume += exportProduct.getSizeHeight() * exportProduct.getSizeWidth() * exportProduct.getSizeHeight() * exportProduct.getCnumber();
                    //毛重=
                    grossWeights += exportProduct.getGrossWeight() * exportProduct.getCnumber();
                    //净重
                    netWeights += exportProduct.getNetWeight() * exportProduct.getCnumber();
                }
            }
        }
        //加入 总体积 总净重 总毛重 合同号集合  装箱费用
        packingList.setExportNos(exportNos);
        packingList.setTotalVolume(totalVolume);
        packingList.setNetWeights(netWeights);
        packingList.setGrossWeights(grossWeights);
        packingList.setPackingMoney(packingList.getGrossWeights() * 520);
        packingList.setShipmentPort(list.get(0));
        packingList.setDestinationPort(list.get(1));
        packingList.setConsignee(list.get(2));
        System.out.println(packingList);
        packingList.setExportIds(id);

        request.setAttribute("packingList", packingList);
        return "cargo/packingList/packingList-add";
    }

    /*
        作用：进入装箱单集合的页面
        路径： /cargo/role/list.do
        参数： 没有
        返回值： cargo/packingList/packingList-list.jsp
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize
    ) {
        //1. 模拟获取登陆的企业id
        String companyId = getLoginCompanyId();
        PackingListExample packingListExample = new PackingListExample();
        PageInfo<PackingList> pageInfo = packingListService.findByPage(packingListExample, pageNum, pageSize);
        System.out.println(pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "cargo/packingList/packingList-list";
    }


    /*
     作用：新增装箱单
     路径： /cargo/packingList/edit.do
     参数： 装箱单
     返回值：装箱单列表
  */
    @RequestMapping("/edit")
    public String edit(PackingList packingList) {
        packingList.setPackingListId(UUID.randomUUID().toString());
        packingList.setCheckBy(getLoginUser().getUserName());
        packingList.setCreateTime(new Date());
        packingList.setState("0");
        packingList.setCompanyId("1");
        packingList.setDeptId(getLoginUser().getDeptId());

       /* //通过装箱单的   报运单ID集合，找出每个报运单
        ExportExample exportExample = new ExportExample();
        String[] split = packingList.getExportIds().split(",");
        exportExample.createCriteria().andIdIn(Arrays.asList(split));
        List<Export> exportList = exportService.findAll(exportExample);
        //遍历每个装箱单是肯定有的，  把状态改成2；
        for (Export export : exportList) {
            export.setState(3);
            exportService.update(export);
        }*/


        packingListService.save(packingList);
        System.out.println("packingList");
        System.out.println("保存成功");
        return "redirect:/cargo/packingList/list.do";
    }


    /*
       作用：进入装箱单的修改页面(该功能不用写)
       路径：
       参数：
       返回值：
    */
    @RequestMapping("/toUpdate")
    public String toUpdate(String packingListId) {
        return "redirect:/cargo/packingList/list.do";
    }

    /*
         作用：删除装箱单
         路径： /carge/packingList/delete.do
         参数： 装箱单的id
         返回值：装箱单列表
     */
    @RequestMapping("/delete")
    public String delete(String packingListId) {
        //1. 删除装箱单
        packingListService.delete(packingListId);
        return "redirect:/cargo/packingList/list.do";
    }


    /*
           作用：生成委托书(第3个功能，还没写)
           路径：
           参数：
           返回值：
       */
    @RequestMapping("/addShipping")
    public String addShipping(String packingListId) {
        //查出装箱单及对应的报运单
        PackingList packingList = packingListService.findById(packingListId);
        String exportIds = packingList.getExportIds();
        String[] exportId = exportIds.split(",");

        Export export = exportService.findById(exportId[0]);
        request.setAttribute("export",export);
        request.setAttribute("packingList",packingList);

        return "cargo/shipping/shipping-add";
    }


    /*
      作用： 查看视图装箱单
      路径： /cargo/packingList/toView.do?id=dd63eb3c-6d4e-4a85-9c37-fcfda1998c1d
      参数： 购销合同id
      返回值：cargo/packingList/packingList-view.jsp
    */
    @RequestMapping("/toView")
    public String toView(String packingListId) {

        PackingList packingList = packingListService.findById(packingListId);
        request.setAttribute("packingList", packingList);
        return "cargo/packingList/packingList-view";
    }


}
